object Loop {
  def main(args: Array[String]): Unit = {
    //案例1
    var num:Int =10
    for (i: Int <- 1 to num) {
//      println(i)
    }

    //案例2
    val ss = List[String]("a","b","c","d","e")
//    println(ss)
    for (s:String <- ss) {
//      println(s)
    }

    //案例3
    for (i:Int <- 1 until 100 ; if i % 2== 0) {
      val y = 2 * i
//      println(y)
    }
//    println(1 until 10)
//    println(1 to 10)

    //map function的使用
    //案例4
//    println(1 to 100 by 5)
    //x:Int 表示1 to 100 by 5的引用
    // x*5 匿名函数
//    var result = (1 to 100 by 5).map {x:Int => x*5}
//    println(result)

//    (1 to 100 by 5).map {x:Int =>
//      val y = x * 2
//      println(y)
//      y
//    }

    //案例5
    var result = (1 to 100 by 5).map {x:Int => x*5}
    def f2(x:Int):Int = 2 * x
    var r1 = (1 to 100).map(f2)  // map{x:Int => 2 * x}函数函数，  f2有名称的函数
//    println(r1)
    var r2 = (1  to 100) map f2 //非常灵活
//    println(r2)

    //案例6
    var v1 = (1 to  100).map{x:Int => if(x%2==0) x} //对数据进行改变(变换)
    println(v1)
//    var v1 = (1 to 100).filter(_%2==0)  //对数据进行过滤
//    var v1 = (1 to 100).filter(_%2==0).map{_*2}  //参数是Int类型， 返回值是boolean
//    println(v1)
//    val v2 = (1 until 30 by 5) filter {_%2==0} map {_*2}
//    println(v2)

    //while循环
//    var num1 = 1
//    while (num1 < 10) {
//      println(num1)
//      num1 = num1+1
//    }

    //do while
//    var num2 = 51
//    do{
//      println(num2)
//      num2 =num2+1
//    }while (num2 < 8)
//    println(test1())

    //break代码测试
    test4()
  }

//  def test1():Int = {
//    var re1 = 100
//     return re1 // return 关键字可以省略
//  }

  def test2():Int = {
    var x = 1
    while (x < 100) {
      if (x == 59) {
        //continue
        //break
      }
      x= x+1
    }
    x
  }

  def test3():Unit = {
    while (true) {
      val rand = Math.random()
      if (rand > 0) {
        println(rand)
      }
    }

    println("Done") //永远不会被执行
  }

  //break的使用
  import scala.util.control.Breaks._
  def test4():Unit = {
    println("start")
    val l = List[Int](1,2,3,4,5)

    //break代码块
    breakable {
      for (x <- l) {
        println(x)
        if (x > 3) break
      }
    }
    println("Done") //会被执行了
  }


}
